//
//  ZLHScrollTextView.m
//  文字滚动
//
//  Created by zhouLiHuan on 2016/12/15.
//  Copyright © 2016年 zhouLiHuan. All rights reserved.
//

#import "ZLHScrollTextView.h"

@implementation ZLHScrollTextView{

    UILabel *_contentLabel1;
    UILabel *_contentLabel2;
    
    NSTimer *_timer;
    
    NSString *_text;
    UIFont *_font;
    UIColor *_textColor;
    
    UITapGestureRecognizer *_tapGesture;
    
    CGFloat _textWidth;
    ZLHTextScrollMode _currentScrollModel;
    ZLHTextScrollMoveDirection _currentMoveDirection;
    
    NSInteger _wanderingOffset;

}

- (id)init{

    NSAssert(NO, @"禁止使用init函数进行控件初始化,请使用initWithFrame:textScrollModel:函数进行控件初始化! ");
    return nil;
}

- (id)initWithFrame:(CGRect)frame{

    NSAssert(NO, @"禁止使用initWithFrame;函数进行控件初始化,请使用initWithFrame:textScrollModel:函数进行初始化!");
    return nil;
}

/**
 *进行控件的初始化
 * @param frame 控件的frame
 * @param scrollModel 字符串的滚动模式
 * @Param moveDirection 字符串的滚动方向
 */
- (id)initWithFrame:(CGRect)frame textScrollModel:(ZLHTextScrollMode)scrollModel direction:(ZLHTextScrollMoveDirection)moveDirection{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        
        //默认初始化
        _text = @"";
        _textColor = [UIColor blackColor];
        _font = [UIFont systemFontOfSize:self.frame.size.height - 4];
        _wanderingOffset = -1;
        
        //设置滚动模式和方向
        _currentScrollModel = scrollModel;
        _currentMoveDirection = moveDirection;
    }
    return self;
}

/**
 * 更改滚动的字符串
 * @param text 字符串的内容
 * @param color 字符串的颜色
 * @param font 字符串的字体
 */

#pragma mark - 开始滚动
-(void)startScrollWithText:(NSString *)text textColor:(UIColor *)color font:(UIFont *)font{

    for (int i = 0; i < self.subviews.count; i ++) {
        UIView *view = [self.subviews objectAtIndex:i];
        [view removeFromSuperview];
        view = nil;
    }
    
    //新赋值
    _text = text;
    _textColor = color;
    _font = font;
    
    _textWidth = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]].width;
    
    //开始滚动
    [self startScroll];
}

#pragma mark - 创建滚动视图
- (void)startScroll{

    if (_text.length == 0) {
        return;
    }
    
    //初始化滚动字符串label
    switch (_currentScrollModel) {
        case ZLHTextScrollContinuous:
            if (_textWidth > self.frame.size.width) {
                if(_currentMoveDirection == ZLHTextScrollMoveLeft){
                    [self creatLabel1AndLabel2WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height) frame2:CGRectMake(_textWidth, 0, _textWidth, self.frame.size.height)];
                }else{
                
                    [self creatLabel1AndLabel2WithFrame:CGRectMake(self.frame.size.width-_textWidth,0, _textWidth, self.frame.size.height) frame2:CGRectMake(self.frame.size.width-_textWidth-_textWidth, 0, _textWidth, self.frame.size.height)];
                }
                
            }else{//如果字符串的长度小于控件宽度,只创建一个字符串label
                if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
                    [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
                }else{
                    [self creatLabel1WithFrame:CGRectMake(self.frame.size.width-_textWidth, 0, _textWidth, self.frame.size.height)];
                }
            }break;
            
        case ZLHTextScrollIntermitent:
        {
            if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
                [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
            }else{
                [self creatLabel1WithFrame:CGRectMake(self.frame.size.width - _textWidth, 0,_textWidth, self.frame.size.height)];
            }
        }break;
            
        case ZLHTextScrollFromOutside:
        {
            if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
                [self creatLabel1WithFrame:CGRectMake(self.frame.size.width, 0, _textWidth, self.frame.size.height)];
            }else{
            
                [self creatLabel1WithFrame:CGRectMake(_textWidth, 0, _textWidth, self.frame.size.height)];
            }
        }break;
        case ZLHTextScrollWanering:
        {
            [self creatLabel1WithFrame:CGRectMake(0, 0, _textWidth, self.frame.size.height)];
        } break;
            
        default:
            break;
    }
    //设置速度,开始滚动 (默认为0.03)
    [self setMoveSpeed:0.03];
}

#pragma mark - 创建label
- (void)creatLabel1WithFrame:(CGRect)frame{
    _contentLabel1 = [[UILabel alloc] initWithFrame:frame];
    _contentLabel1.text = _text;
    _contentLabel1.font = _font;
    _contentLabel1.textColor = _textColor;
    _contentLabel1.backgroundColor = [UIColor clearColor];
    [self addSubview:_contentLabel1];
}

- (void)creatLabel1AndLabel2WithFrame:(CGRect)frame1 frame2:(CGRect)frame2{

    _contentLabel1 = [[UILabel alloc] initWithFrame:frame1];
    _contentLabel1.text = _text;
    _contentLabel1.font = _font;
    _contentLabel1.textColor = _textColor;
    _contentLabel1.backgroundColor = [UIColor clearColor];
    [self addSubview:_contentLabel1];
    
    _contentLabel2 = [[UILabel alloc] initWithFrame:frame2];
    _contentLabel2.text = _text;
    _contentLabel2.font = _font;
    _contentLabel2.textColor = _textColor;
    _contentLabel2.backgroundColor = [UIColor clearColor];

}
/**
 * 设置字符串的移动速度
 * @param speed 移动速度 取值越小速度越快 取值范围:0.001~0.1
 */
-(void)setMoveSpeed:(CGFloat)speed{
    if(speed>0.1){
        [_timer invalidate];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(contentMove) userInfo:nil repeats:YES];
        return;
    }
    if (speed<0.001) {
        [_timer invalidate];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(contentMove) userInfo:nil repeats:YES];
        return;
    }

    [_timer invalidate];
    _timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(contentMove) userInfo:nil repeats:YES];
}

//内容移动
- (void)contentMove {

    if(_textWidth < self.frame.size.width ){//如果字符串长度小于控件的宽度 不滚动
        if (_currentScrollModel ==ZLHTextScrollWanering) { //往返模式除外
            
            [self moveWandering]; //往返滚动
            return;
        }
    }
        switch (_currentScrollModel) {
            case ZLHTextScrollContinuous:
            {
                [self moveContinuous]; //连续滚动
            }
                break;
           case ZLHTextScrollIntermitent:
            {
                [self moveIntermittent];//间断滚动
            }
            case ZLHTextScrollFromOutside:
            {
                [self moveFromOutSide];//控件外开始滚动
            }
            case ZLHTextScrollWanering:
            {
                [self moveWandering]; //往返滚动
            }
            default:
                break;
        }
        
    }

//连续滚动
- (void)moveContinuous{
    if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x - 1, 0, _textWidth, self.frame.size.height);
        _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x -1,0, _textWidth, self.frame.size.height);
        if (_contentLabel1.frame.origin.x < -_textWidth) {
            _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x + _textWidth, 0, _textWidth, self.frame.size.height);
        }
        
        if (_contentLabel2.frame.origin.x < -_textWidth) {
            _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x + _textWidth, 0, _textWidth, self.frame.size.height);
        }
    }else{
    
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + 1, 0, _textWidth, self.frame.size.height);
        _contentLabel2.frame = CGRectMake(_contentLabel2.frame.origin.x + 1, 0, _textWidth, self.frame.size.height);
        if (_contentLabel1.frame.origin.x > _textWidth) {
            _contentLabel1.frame = CGRectMake(_contentLabel2.frame.origin.x - _textWidth, 0, _textWidth, self.frame.size.height);
        }
        if(_contentLabel2.frame.origin.x > _textWidth){
            _contentLabel2.frame = CGRectMake(_contentLabel1.frame.origin.x, 0, _textWidth, self.frame.size.height);
        }
    }
}
//往返滚动
- (void)moveWandering{
    _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x +_wanderingOffset, 0, _textWidth, self.frame.size.height);
    if (_contentLabel1.frame.origin.x < -(_textWidth-self.frame.size.width)) {
        _wanderingOffset = 1;
    }
    if (_contentLabel1.frame.origin.x > 2) {
        _wanderingOffset = -1;
    }
    
}

//间断滚动
- (void)moveIntermittent{
    if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x - 1, 0, _textWidth, self.frame.size.height);
        if (_contentLabel1.frame.origin.x < -_textWidth) {
            _contentLabel1.frame = CGRectMake(0, 0, _textWidth, self.frame.size.height);
        }
    }else{
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + 1, 0, _textWidth, self.frame.size.height);
        if (_contentLabel1.frame.origin.x > self.frame.size.width) {
            _contentLabel1.frame = CGRectMake(self.frame.size.width-_textWidth, 0, _textWidth, self.frame.size.height);
        }
    }
}
//控件外开始滚动
- (void)moveFromOutSide{
    if (_currentMoveDirection == ZLHTextScrollMoveLeft) {
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x - 1, 0, _textWidth, self.frame.size.height);
        if (_contentLabel1.frame.origin.x < -_textWidth) {
            _contentLabel1.frame = CGRectMake(self.frame.size.width, 0, _textWidth, self.frame.size.height);
        }
    }else {
        _contentLabel1.frame = CGRectMake(_contentLabel1.frame.origin.x + 1, 0, _textWidth, self.frame.size.height);
        
        if (_contentLabel1.frame.origin.x > self.frame.size.width) {
            _contentLabel1.frame = CGRectMake(-_textWidth, 0, _textWidth, self.frame.size.height);
        }
    }
}
@end
